package com.demo.spring.controller;

import com.demo.spring.service.HelloService;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wl
 * @since 2018/4/16
 */
@Controller
@RequestMapping("/hello")
public class HelloController {

  @Resource
  private Environment env;

  @Resource
  private HelloService helloService;

  /**
   * Note that actual processing of the @Value annotation is performed by a BeanPostProcessor
   * BeanPostProcessor interfaces are scoped per-container.
   * This is only relevant if you are using container hierarchies.
   * If you define a BeanPostProcessor in one container,
   * it will only do its work on the beans in that container.
   * Beans that are defined in one container are not post-processed by a BeanPostProcessor in another container,
   * even if both containers are part of the same hierarchy.
   *
   * 也就是说这个 @Value 的注解的处理，是在 BeanPostProcessor 中处理的，而它是每个 container 中独立的，
   * 那么在 root application context 中加载的配置文件，就无法在 spring servlet context 中取到
   **/
  @Value("${test.prop}")
  private String test;

  @PostConstruct
  public void init() {
    System.out.println(getClass().getName() + " init !");
  }

  @RequestMapping("/world")
  @ResponseBody
  public String test() {
    helloService.hello();
    System.out.println("controller @Value ---- " + test);
    System.out.println("controller env ---- " + env.getProperty("test.prop"));
    return "hello world!";
  }
}